package com.gxa.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 功能描述：解决跨域过滤器
 * 2017年9月4日
 */
public class Myfilter implements Filter{


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest erequest, ServletResponse eresponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)erequest;
        HttpServletResponse response = (HttpServletResponse)eresponse;
        //允许跨域主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域方法
        response.setHeader("Access-Control-Allow-Methods", "POST, DELETE,PUT,GET,OPTIONS");
        //缓存时间
        response.setHeader("Access-Control-Max-Age", "3600");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");
        //允许放行
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}